/*
 * Texture.h
 * texture object
 *  Created on: Oct 10, 2012
 *      Author: pigmal
 */

#ifndef TEXTURE_H_
#define TEXTURE_H_
#include <GLES/gl.h>
#include "MAHeaders.h"

class Texture
{
public:
	Texture()
	{
	    // Define texture coordinates
		TexVert[0][0] = 1.0f;  TexVert[0][1] = 1.0f;	//C
		TexVert[1][0] = 0.0f;  TexVert[1][1] = 1.0f;	//B
		TexVert[2][0] = 0.0f;  TexVert[2][1] = 0.0f;	//A
		TexVert[3][0] = 1.0f;  TexVert[3][1] = 0.0f;	//D
	}
	~Texture(){;}

	bool Setup(MAHandle resource, GLfixed param)
	{
		Resource = resource;

		// Create an OpenGL 2D texture from the resource.
		glEnable(GL_TEXTURE_2D);
		glGenTextures(1, &TexHandle);
		glBindTexture(GL_TEXTURE_2D, TexHandle);
		maOpenGLTexImage2D(resource);

		// Set texture parameters.
		glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, param);
		glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, param);
	}

	void Draw()
	{
	    // Enable texture coord array
	    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

		//glBindTexture(GL_TEXTURE_2D, TexHandle);

		//saet pointer to texture coordinates
	    glTexCoordPointer(2, GL_FLOAT, 0, TexVert);

	    // Array used to convert from QUAD to TRIANGLE_STRIP.
	    // QUAD is not available on the OpenGL implementation
	    // we are using.
	    GLubyte indices[4] = {0, 1, 3, 2};//C,B,D,A

	    // This draws one textured plane using a strip of two triangles.
	    glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices);
	}

private:

	static const int TEX_VERT_1D = 4;
	static const int TEX_VERT_2D = 2;

    GLfloat TexVert[TEX_VERT_1D][TEX_VERT_2D];	//tex vertices.

	MAHandle Resource;
	GLuint TexHandle;
};
#endif /* TEXTURE_H_ */
